//
//  BCFeedbackController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/15.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCFeedbackController.h"

@interface BCFeedbackController ()

@property (nonatomic, strong) QMUIButton *safariButton;

@end

@implementation BCFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setUpSafariButton];
    //
    [self loadRequestWithURL:[NSURL URLWithString:self.helpUrl]];
}
- (void)setUpSafariButton {
//    general_safari
    self.safariButton = [QMUIButton new];
    [self.safariButton setImage:[LOAD_IMAGE(@"general_safari") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [self.customNaviBar addSubview:self.safariButton];
    self.safariButton.alpha = 0;
    //
    [self.safariButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.customNaviBar.rightButton.mas_centerY);
        make.right.mas_equalTo(self.customNaviBar).offset(-10.0);
    }];
    self.customNaviBar.titleRightNearView = self.safariButton;
    [self.customNaviBar layoutSubviews];
    //
    self.safariButton.tintColor = self.customNaviBar.rightButton.tintColor;
    //
    [self.safariButton addTarget:self action:@selector(safariButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}
//
#pragma mark - safariButtonDidClickedAction
- (void)safariButtonDidClickedAction {
    NSURL *openUrl = self.currentRequest.URL;
    if (@available(iOS 10.0,*)) {
        [[UIApplication sharedApplication] openURL:openUrl options:@{} completionHandler:nil];
    }else {
        [[UIApplication sharedApplication] openURL:openUrl];
    }
}

#pragma mark - override
// 当main frame导航完成时，会回调
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    [super webView:webView didFinishNavigation:navigation];
    //是否需要加入叉叉按钮
//    if ([webView canGoBack]) {
//        //需要  判断是否已经加上去了
//        self.customNaviBar.rightButton.alpha = 1;
//    }else {//不需要就不处理了
//        self.customNaviBar.rightButton.alpha = 0;
//    }
    self.safariButton.alpha = 1;
    if (self.customNaviBar.rightButton.alpha) {
        [self.safariButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.customNaviBar.rightButton.mas_centerY);
            make.right.mas_equalTo(self.customNaviBar.rightButton.mas_left).offset(-10.0);
        }];
    }else {
        [self.safariButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.customNaviBar.rightButton.mas_centerY);
            make.right.mas_equalTo(self.customNaviBar).offset(-10.0);
        }];
    }
    [self.customNaviBar layoutIfNeeded];
    [self.customNaviBar layoutSubviews];
}



@end
